package widget.book.viewer
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import widget.book.manager.BookManager;

	public class CoverViewer extends BasePageViewer{
		
		private var rect:Rectangle = null;
		
		private var bookManager:BookManager = null;
		private var pageType:String = "";
		public function CoverViewer(type:String)
		{
			super();
			pageType = type;
			pageWidth = bookManager.bookModel.width / 2;
			pageHeight = bookManager.bookModel.height;
			
			pageBitmapData = new BitmapData(pageWidth, pageHeight);
			pageBitmap = new Bitmap();
			
			rect = new Rectangle( 0, 0,pageWidth,pageHeight);
		}
		
		protected override function completeHandler(event:Event):void {
			uLoader.scrollRect = rect;
			pageBitmapData.draw(uLoader);
			pageBitmap.bitmapData = pageBitmapData;
			
			if(pageType == BookViewer.TYPE_PAGE_BACK){
				
			}else if(pageType == BookViewer.TYPE_PAGE_FRONT){
				pageBitmap.x = -pageWidth;
			}
			
			addChild(pageBitmap);
			
			//dispatchEvent(new FlipEvent(FlipEvent.COMPLETE));
		}
		
		public override function set position(value:Number):void {
			switch (value) {
				case 0:
					pageBitmap.y = 0;
					break;
				case 2:
					pageBitmap.y = -pageHeight;
					break;
				case 1:
					pageBitmap.y = 0;
					break;
				case 3:
					pageBitmap.y = -pageHeight;
					break;
				default:
					break;
			}
		}
		
		public override function get sourceObject():DisplayObject {
			return uLoader;
		}
		
		public override function clone():BasePageViewer {
			var p:CoverViewer = new CoverViewer(pageType);
			return p;
		}
	}
}